import pdfkit
import os
# 把所有html文件转换成pdf文件
def save_pdf(htmls,file_name):
    options = {
        'page-size': 'Letter',
        'margin-top': '0.75in',
        'margin-right': '0.75in',
        'margin-bottom': '0.75in',
        'margin-left': '0.75in',
        'encoding': "UTF-8",
        # 'custom-header': [
        #     ('Accept-Encoding', 'gzip')
        # ],
        'cookie': [
            ('cookie-name1', 'cookie-value1'),
            ('cookie-name2', 'cookie-value2'),
        ],
        'outline-depth': 10,
    }
    pdfkit.from_file(htmls, file_name, options=options)

inPath = input('请输入html文件路径：')
while not os.path.exists(inPath):
    print('路径错误：文件不存在（请输入绝对路径）')
    inPath = input('请输入html文件路径：')
fName = os.path.basename(inPath)
fName = fName[:-4]+'pdf'
# print(fName)
save_pdf(inPath,"./"+fName)
input('按任意键结束！')